<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<link rel="stylesheet" href="plugins/kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="plugins/kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="plugins/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="plugins/kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="plugins/kindeditor/plugins/code/prettify.js"></script>
<script charset="utf-8" src="js/articleEditor.js"></script>

<style type="text/css">

</style>

<%-- 发表文章界面 --%>

<%-- 半透明背景DIV --%>
<div id="inputBGDiv" style="display: none;position: absolute;top: 0px;left: 0px;background-color: #EEEEEE;z-index: 800;"></div>
<%-- 悬浮弹出回复DIV --%>
<div id="inputDiv" style="width: 772px;height: auto;border: 1px solid #CCCCCC;display: none;position: absolute;top: 0px;left: 0px;background-color: #DEF3F8;z-index: 801;">
	<div style="float: left;width: 772px;height: 30px;line-height: 30px;text-align: center;overflow: hidden;background-color: #0080C0;">
		<div style="float: left;color: white;width: 400px;height: 30px;font-weight: bold;text-align: left;margin-left: 10px;">发表新鲜事</div>
		<div style="float: right;color: white;font-weight: bold;width: 30px;height: 30px;cursor: pointer;" onclick="hideInputDiv()">×</div>
	</div>				
	<div id="indexArticleListForMapPointDiv" class="indexArticleListForMapPointDiv">
		
	</div>

	<div class="margin_5"></div>
	<div style="float: left;width: 772px;height: 30px;line-height: 30px;text-align: left;">
		标题：<input id="articleTitle" type="text" style="width: 200px;">(15字以内)
	</div>
	<div class="margin_5"></div>			
	<div style="float: left;width: 772px;height: 220px;overflow: hidden;overflow: hidden;">
		<textarea id="inputTextArea" name="inputTextArea" cols="100" rows="8" style="width:770px;height:180px;"></textarea>
	</div>
	<div style="float: left;width: 772px;height: 25px;margin-top: 3px;">
		<input type="button" onclick="submitArticle()" value="提交新鲜事" style="float: left;height: 25px;line-height: 20px;font-weight: bold;background-color: #2B7BCC;cursor: pointer;color: white;border: 1px solid gray;">
	</div>
	<div class="margin_10"></div>
	<div style="float: left;width: 772px;height: auto;text-align: left;border-bottom: 1px dashed #CCCCCC;">
		<div style="float: left;width: 700px;height: 30px;line-height: 30px;">打上标签：</div>
		<div style="clear: both;float: left;width: 60px;height: 30px;line-height: 30px;">热门标签：</div>
		<div id="indexArticleHotTagListDiv" style="float: left;width: 600px;height: auto;line-height: 30px;border: 1px solid #DDDDDD;">
			
		</div>
		<div style="float: left;width: 60px;height: 30px;line-height: 30px;"><a href="#none">更多标签</a></div>
		<div style="float: left;width: 700px;height: auto;text-align: left;">
			创建标签：<input id="tagInputValue" type="text">(此版本在此输入标签名,多个标签请以‘#’隔开)
		</div>
	</div>
	<div class="margin_100"></div>
</div>	

<%-- Javascript 区域 --%>
<script type="text/javascript">

//
//---------------------------------------- 全局变量 ------------------------------------------------
//
var partCreateArticleMethod_selectArticleImplementation = function() { alert("partCreateArticleMethod_selectArticleImplementation is not registered"); };
var partCreateArticleMethod_getPositionInfoImplementation = function() { alert("partCreateArticleMethod_getPositionInfoImplementation is not registered"); };

<%
	String methodName_selectArticleImplementation = request.getParameter("methodName_selectArticleImplementation");
	String methodName_getPositionInfoImplementation = request.getParameter("methodName_getPositionInfoImplementation");
	
	if (methodName_selectArticleImplementation != null) {
%>
		partCreateArticleMethod_selectArticleImplementation = <%= methodName_selectArticleImplementation %>;
<%
	}
	
	if (methodName_getPositionInfoImplementation != null) {
%>
		partCreateArticleMethod_getPositionInfoImplementation = <%= methodName_getPositionInfoImplementation %>;
<%		
	}
%>

//
//---------------------------------------- 自动执行代码 ------------------------------------------------
//

// 暂无

//
//---------------------------------------- 对外公开的函数 ------------------------------------------------
//

	//显示弹出编辑器DIV
	function partCreateArticleMethod_showCreationDiv() {
		$('#inputBGDiv').css({
			'display':'block',
			'width':$(document.body).width(),
			'height':$(document.body).height(),
			'opacity':'0.8'
		});
		var divW = $('#inputDiv').width();
		var divH = $('#inputDiv').height();
		var windowW = $(window).width();
		var windowH = $(window).height();
		var scrollH = $(document).scrollTop();
		$('#inputDiv').css({
			'display':'block',
			//'top':scrollH+((windowH-divH)/2),
			'top':0,
			'left':(windowW-divW)/2
		});
		initIndexArticleHotTagList();
	}
	


//
//---------------------------------------- 私有函数 ------------------------------------------------
//
	


	//提交添加文章（新鲜事）
	function submitArticle() {
		var articleContent = articleEditor_inputEditor.html();
		var articleSummary = articleEditor_inputEditor.text().substring(0,20);
		var articleTitle = $('#articleTitle').val();
		var tagValue = $('#tagInputValue').val();
		
		var posiInfoArray = partCreateArticleMethod_getPositionInfoImplementation();
		// This Array must match [mapX, mapY, mapAddr, mapDesc, mapProvider, mapType]
		if (posiInfoArray.length != 6)
		{
			alert("无法查询当前位置信息！");
			return;
		}
		else {
			var mapX = posiInfoArray[0];
			var mapY = posiInfoArray[1];
			var mapAddr = posiInfoArray[2];
			var mapDesc = posiInfoArray[3];
			var mapProvider = posiInfoArray[4];
			var mapType = posiInfoArray[5];
			
			// alert("Creation postion: " + mapX+"-"+ mapY+"-"+ mapAddr+"-"+ mapDesc+"-"+ mapProvider+"-"+ mapType);
		}
		
		$('input[name="selectTags"]:checked').each(function() {
			if(tagValue != null && tagValue != "") {
				tagValue += "#";
			}
			tagValue += $(this).val();
		});
		if(tagValue == null || tagValue == "") {
			alert("请打上你的标签！^_^");
			return;
		}
		
		$.ajax({
			url: 'ajaxAddArticle.action',
			data: {
				articleTitle: articleTitle,
				articleContent: articleContent,
				articleSummary: articleSummary,
				tagValue: tagValue,
				mapX: mapX,
				mapY: mapY,
				mapAddr: mapAddr,
				mapDesc: mapDesc,
				mapProvider: mapProvider,
				mapType: mapType
			},
			type: 'post',
			async: false,
			dataType: 'json',
			success: function(res) {
				var rs = res.result;
				if(rs == 0) {
					alert("新鲜事发表成功！");
					hideInputDiv();
					//刷新index文章列表
					partCreateArticleMethod_selectArticleImplementation();
				}else {
					alert("对不起发表失败！请回到主页刷新后重试.");
				}
			}
		});
		
		
	}
	


	//初始化发表文章弹出框中的标签选项
	function initIndexArticleHotTagList() {
		$.ajax({
			url: 'ajaxQueryHotTagList.action',
			data: null,
			type: 'post',
			async: true,
			dataType: 'json',
			success: function(res) {
				var rs = res.result;
				var tagListContent = '';
				if(rs == 0) {
					for(var i=0;i<res.data.length;i++) {
						var item = res.data[i];
						tagListContent += 
							'<div style="float: left;width: 95px;height: 30px;line-height: 30px;">'+
							'<input type="checkbox" name="selectTags" value="'+item.tagName+'">'+item.tagName+''+
							'</div>';
					}
					$('#indexArticleHotTagListDiv').html(tagListContent);
				}
			}
		});
	}

</script>